<template>
  <div>
    <div align="center" style="color: red">
      <h2 v-show="!ok">已打卡</h2>
    </div>
    <el-form :model="dkForm" :disabled="alldisabled" :label-position="labelPosition" :rules="rules" label-width="80px">
      <el-form-item label="打卡日期" prop="date">
        <el-input v-model="dkForm.full_date" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dkForm.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="id">
        <el-input v-model="dkForm.id_card" ></el-input>
      </el-form-item>
      <el-form-item label="身份" prop="status">
        <el-input v-model="dkForm.status"></el-input>
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-input v-model="dkForm.campus"></el-input>
      </el-form-item>
      <el-form-item label="籍贯" prop="birthplace">
        <el-input v-model="dkForm.birthplace"></el-input>
      </el-form-item>
      <el-form-item label="居住地" prop="home">
        <el-input v-model="dkForm.home"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dkForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="身体状况" prop="health">
        <el-input v-model="dkForm.health"></el-input>
      </el-form-item>
      <el-form-item label="当前所在地" prop="location">
        <el-input v-model="dkForm.location"></el-input>
      </el-form-item>
      <el-form-item label="是否经过高风险地区" prop="high_risk_areas">
        <el-select v-model="dkForm.high_risk_areas" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="体温" prop="temperature">
        <el-input v-model="dkForm.temperature"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-show="ok" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        labelPosition: 'top',
        ok: true,
        disabled: true,
        alldisabled: false,
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
          id: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
           ],
          status: [
            { required: true, message: '请输入身份', trigger: 'blur' },
          ],
          campus: [
            { required: true, message: '请输入校区', trigger: 'blur' },
           ],
          birthplace: [
            { required: true, message: '请输入籍贯', trigger: 'blur' },
           ],
          home: [
            { required: true, message: '请输入居住地', trigger: 'blur' },
           ],
          phone: [
            { required: true, message: '请输入联系电话', trigger: 'blur' },
           ],
          health: [
            { required: true, message: '请输入身体状况', trigger: 'blur' },
            ],
          location: [
            { required: true, message: '请输入当前所在地', trigger: 'blur' },
          ],
          high_risk_areas: [
            { required: true, message: '请选择', trigger: 'blur' },
          ],
          temperature: [
            { required: true, message: '请填写体温', trigger: 'blur' },
            { type: 'number', message: '必须为数字值'}
          ],
        },
        dkForm: {
          id: null,
          full_date: null,
          name: null,
          id_card: null,
          status: null,
          campus: null,
          birthplace: null,
          home: null,
          phone: null,
          health: null,
          location: null,
          high_risk_areas: null,
          temperature: null,
          finish: null,
        },
        options: [{
          value: 1,
          label: '是'
        }, {
          value: 0,
          label: '否'
        },
        ],
      };
    },
    created() {
      this.initForm();
    },
    methods: {
      initForm() {
        this.dkForm.id = Number(sessionStorage.getItem("id"));
        this.$axios.get('/yqdk/get',{
          params: {
            id: this.dkForm.id,
          }
        }).then((response)=>{
          this.dkForm = response.data;
          if (this.dkForm.finish === 1){
            this.ok = false;
            this.alldisabled = true;
          }
        }).catch(()=>{
            // this.$router.push({path:'/'});
            // location.reload();
          this.$router.replace({path: '/'});
          location.reload();
        });
      },
      submitForm() {
        this.dkForm.finish = 1;
        let formData = JSON.stringify(this.dkForm);
        this.$axios({
          method: 'post',
          url: '/yqdk/add',
          headers: {
            "Content-Type": "application/json"  //传json数据要加这个，不然导致后端接收的数据与前端不一致
          },
          data: formData,
        }).then(()=>{
          this.ok = false;
          this.alldisabled = true;
          window.alert("提交成功！");
        });
      },
    }
  }

</script>

<style scoped>

</style>
